import { createSlice } from '@reduxjs/toolkit'

import Storage from '../util/localStorage'
const user = createSlice({
    name: "user",
    initialState: {
        userObj: Storage.getStorage('userObj') || {},
        isLogin: Storage.getStorage('isLogin') || false,
        menuList:Storage.getStorage("menuList") || [],
    },
    reducers: {
        addUserList(state, { payload }) {
            state.userObj = payload
            Storage.setStorage('userObj', state.userObj)
        },
        changeIsLogin(state, { payload }) {
            state.isLogin = payload
            Storage.setStorage('isLogin', state.isLogin)
        },
        addMenuList(state,{payload}){
            state.menuList = payload
            Storage.setStorage('menuList',state.menuList)
        },
        removeLogin(state,{payload}){
            state.userObj = {}
            state.isLogin = false
            
            Storage.setStorage('userObj', state.userObj)
            Storage.setStorage('isLogin', state.isLogin)
        
        }
    }
})

export const { addUserList,changeIsLogin,addMenuList,removeLogin } = user.actions
export default user.reducer
